//
//  JACustomKeyboard.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/13.
//  Copyright © 2017年 Arvin. All rights reserved.
//

import UIKit

fileprivate let identifier = "emotion"

fileprivate class EmotionLayout: UICollectionViewFlowLayout {
    override func prepare() {
        
         //设置layout
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        itemSize = CGSize(width: screenWidh, height: 150)
        //水平滚动
        scrollDirection = .horizontal
    }
}

class JACustomKeyboard: UIView {
    
    var dataSourceArr = JAEmotionTool.shared.dataSourceArr
    
    /// 表情的collectionView
    lazy var emotionView: UICollectionView = {
       
        //创建collectionView
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: EmotionLayout())
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(JAEmotionCell.self, forCellWithReuseIdentifier: identifier)
        //开启分页效果
        collectionView.isPagingEnabled = true
        //隐藏水平滚动条
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = UIColor.rgbColor(r: 243, g: 243, b: 243)
        
        return collectionView
    }()
    
    ///pageControl分页指示器
    lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.isEnabled = false
        pageControl.numberOfPages = self.dataSourceArr.count
        //当前页
        pageControl.currentPage = 0
        //默认的小图片
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        //选中的小图片
         pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        
        return pageControl
    }()
    
    /// 底部的toolBar
    lazy var toolBar: JAKeyboardToolbar = {
        let toolBar = JAKeyboardToolbar()
        toolBar.delegate = self
        
        return toolBar
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension JACustomKeyboard: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSourceArr[section].count
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return dataSourceArr.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! JAEmotionCell
        
        cell.emotions = dataSourceArr[indexPath.section][indexPath.item]
        
        return cell
    }
}

extension JACustomKeyboard {
    func setupUI () {
        addSubview(emotionView)
        addSubview(pageControl)
        addSubview(toolBar)
        
       
        
        emotionView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(150)
        }

        pageControl.snp.makeConstraints { (make) in
            make.top.equalTo(emotionView.snp.bottom)
            make.height.equalTo(29)
            make.left.right.equalTo(self)
            make.centerX.equalTo(self)
        }
        
        toolBar.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(37)
        }

    }
    
    func setupPageControl(indexPath: IndexPath) {
        pageControl.numberOfPages = dataSourceArr[indexPath.section].count
        pageControl.currentPage = indexPath.item
        
        
    }
}

extension JACustomKeyboard: JAKeyboardToolbarDelegate {
    func toggleKeyboard(section: Int) {
        let indexPath = IndexPath(item: 0, section: section)
        emotionView.scrollToItem(at: indexPath, at: .left, animated: false)
        setupPageControl(indexPath: indexPath)
    }
}


extension JACustomKeyboard: UICollectionViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //拿到两个cell
        let cells = emotionView.visibleCells
        
        //获取cell在屏幕上的显示区域
        if cells.count > 1 {
            let cell1 = cells[0]
            let cell2 = cells[1]
            
            //判断offset与cell的originx的差值的绝对值, 绝对值越小, 显示区域越大
            let offsetX = scrollView.contentOffset.x
            let origin1 = abs(offsetX - cell1.frame.origin.x)
            let origin2 = abs(offsetX - cell2.frame.origin.x)
            
            //当前显示的cell
            if origin1 < origin2 {
                let indexPath1 = emotionView.indexPath(for: cell1)
                toolBar.selectedIndex = indexPath1!.section
                setupPageControl(indexPath: indexPath1!)
            } else {
                let indexPath2 = emotionView.indexPath(for: cell2)
                toolBar.selectedIndex = indexPath2!.section
                setupPageControl(indexPath: indexPath2!)
            }
        }
        

    }
}
